<template>
  <vxe-modal
    v-model="visible"
    ref="mymodal"
    showFooter
    esc-closable
    title="新增物料"
    width="540"
    @close="closeFrom"
    @hide="closeFrom"
    remember
  >
    <div class="form-contianer">
      <el-form ref="newForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物料名称" prop="name">
          <el-input
            v-model="form.name"
            name="name"
            placeholder="物料名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="物料编码" prop="code">
          <el-input
            v-model="form.code"
            name="code"
            placeholder="物料编码"
          ></el-input>
        </el-form-item>
        <el-form-item label="规格" prop="spec">
          <el-input
            v-model="form.spec"
            name="spec"
            placeholder="规格"
          ></el-input>
        </el-form-item>
        <el-form-item label="计量单位" prop="value2">
          <el-select
            v-model="form.value2"
            placeholder="请选择计量单位"
            @change="changeUnits"
          >
            <el-option
              v-for="item in units"
              :key="item.id"
              :label="item.name"
              :value="item.code"
            >
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 12px">{{
                item.code
              }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="别名" prop="alias">
          <el-input v-model="form.alias" placeholder="选填"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeFrom" size="small">取 消</el-button>
      <el-button
        type="primary"
        @click.native.prevent="submitHandle"
        :loading="loading"
        size="small"
        >创 建</el-button
      >
    </div>
  </vxe-modal>
</template>

<script>
import { add } from '@/api/supplies'
export default {
  name: 'AddSupplies',
  props: {
    addDialog: {
      type: Boolean
    },
    units: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  watch: {
    addDialog(val) {
      this.visible = val
    }
  },
  created() {},
  data() {
    return {
      visible: false,
      loading: false,
      form: {
        name: '',
        code: '',
        spec: '',
        alias: '',
        value2: 'gongjin'
      },

      rules: {
        name: [{ required: true, message: '请输入物料名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入物料编码', trigger: 'blur' }],
        spec: [{ required: true, trigger: 'blur', message: '请输入规格' }],
        value2: [{ required: true, trigger: 'blur', message: '请选择计量单位' }]
      }
    }
  },
  methods: {
    closeFrom() {
      this.$emit('update:addDialog', false)
    },
    submitHandle() {
      const that = this
      this.$refs.newForm.validate((valid) => {
        if (valid) {
          this.loading = true
          add(that.form)
            .then((res) => {
              this.loading = false
              this.$message({
                message: res.msg,
                type: 'success'
              })
              setTimeout(() => {
                that.closeFrom()
              }, 1000)
            })
            .catch((res) => {
              this.loading = false
            })
        } else {
          this.loading = false
          return false
        }
      })
    },
    changeUnits(e) {
      console.log(e)
      // this.$emit('update:value2', e)
    }
  }
}
</script>

<style lang="scss" scoped>
.form-contianer {
  padding: 25px 30px;
}
</style>
